//
//  XSVideoWaterPrintEditeView.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/9/5.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSVideoWaterPrintEditeView.h"


@interface XSVideoWaterPrintEditeView () <UITextViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property(nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic, strong) UITextView *textView;
@property(nonatomic,strong)UIButton *selectPhotoBtn;

@property(nonatomic,strong) NSMutableArray *colorsArray;
@property(nonatomic,strong) UIColor *selectedColor;

@end

@implementation XSVideoWaterPrintEditeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    self.selectedColor = self.colorsArray[0];
    
    UIButton *cancelBtn = [self createButtonWithTitle:@"取消" AndFrame:CGRectMake(15, 64, 50, 40)];
    [cancelBtn addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    
    UIButton *confirmBtn = [self createButtonWithTitle:@"完成" AndFrame:CGRectMake(self.frame.size.width - 65, 64, 50, 40)];
    [confirmBtn setTitleColor:self.selectedColor forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:confirmBtn];
    self.confirmBtn = confirmBtn;
    
    self.textView = [[UITextView alloc]initWithFrame:CGRectMake(15, 100, self.frame.size.width - 30, 160 )];
    self.textView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    self.textView.delegate = self;
    self.textView.font = [UIFont systemFontOfSize:20];
    self.textView.textColor = self.selectedColor;
    [self addSubview:self.textView];
    [self.textView becomeFirstResponder];
    
    CGFloat wid = self.textView.width /(self.colorsArray.count + self.colorsArray.count - 1);
    
    for (int i = 0; i<self.colorsArray.count; i++) {
        CGFloat x = self.textView.left + i * (wid + wid);
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(x , self.textView.bottom + 25, wid, wid)];
        btn.tag = i;
        btn.backgroundColor = self.colorsArray[i];
        btn.layer.cornerRadius = wid/2;
        btn.layer.masksToBounds = YES;
        [btn addTarget:self action:@selector(colorBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }
    
    self.selectPhotoBtn.frame = CGRectMake(self.textView.right - 40, self.textView.bottom + wid + 25 + 10, 40, 40);
    
}

- (UIButton *)createButtonWithTitle:(NSString *)title AndFrame:(CGRect)frame {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    return button;
}
- (NSMutableArray *)colorsArray{
    if (!_colorsArray) {
        _colorsArray = [NSMutableArray array];
        [_colorsArray addObject:[UIColor redColor]];
        [_colorsArray addObject:[UIColor orangeColor]];
        [_colorsArray addObject:[UIColor yellowColor]];
        [_colorsArray addObject:[UIColor greenColor]];
        [_colorsArray addObject:[UIColor blueColor]];
        [_colorsArray addObject:[UIColor cyanColor]];
        [_colorsArray addObject:[UIColor purpleColor]];
        [_colorsArray addObject:[UIColor blackColor]];
        [_colorsArray addObject:[UIColor whiteColor]];
        
//        _colorsArray = [NSMutableArray arrayWithArray: @[[UIColor blackColor],
//                        [UIColor colorWithRed:199.0/255.0 green:192.0/255.0 blue:62.0/255.0 alpha:1.0],
//                        [UIColor colorWithRed:197.0/255.0 green:138.0/255.0 blue:56.0/255.0 alpha:1.0],
//                        [UIColor colorWithRed:197.0/255.0 green:48.0/255.0 blue:51.0/255.0 alpha:1.0],
//                        [UIColor colorWithRed:194.0/255.0 green:68.0/255.0 blue:126.0/255.0 alpha:1.0],
//                        [UIColor colorWithRed:132.0/255.0 green:55.0/255.0 blue:196.0/255.0 alpha:1.0],
//                        [UIColor colorWithRed:48.0/255.0 green:66.0/255.0 blue:196.0/255.0 alpha:1.0],
//                        [UIColor colorWithRed:55.0/255.0 green:171.0/255.0 blue:197.0/255.0 alpha:1.0],
//                        [UIColor colorWithRed:56.0/255.0 green:197.0/255.0 blue:69.0/255.0 alpha:1.0]
//                        ]];
        
    }
    return _colorsArray;
}

- (void)colorBtnClicked:(UIButton *)btn{
    self.selectedColor = self.colorsArray[btn.tag];
    
    self.textView.textColor = self.selectedColor;
    [self.confirmBtn setTitleColor:self.selectedColor forState:UIControlStateNormal];
}

- (UIButton *)selectPhotoBtn{
    if (!_selectPhotoBtn) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:[UIImage imageNamed:@"photoLibrary"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(selectPhotoBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        _selectPhotoBtn = btn;
    }
    return _selectPhotoBtn;
}

- (void)selectPhotoBtnClicked:(UIButton *)btn{
//    [self.textView resignFirstResponder];
    
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//    imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeMovie, nil];
    imagePickerController.delegate = self;
    imagePickerController.editing = YES;
    [KeyWindow.rootViewController presentViewController:imagePickerController animated:YES completion:nil];
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    UIImage *compressedImage = [image compressedImage:CGSizeMake(200, 200)];
    
    if (self.selectImageFinishBlock) {
        self.selectImageFinishBlock(compressedImage);
    }
    
    [self cancelButtonAction];
}

-(void)setTextString:(NSString *)textString {
    _textString = textString;
    self.textView.text = _textString;
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    
}
- (void)textViewDidEndEditing:(UITextView *)textView {
    self.textView.text = textView.text;
}

#pragma mark - Action
- (void)cancelButtonAction {
    if (self.cancelEditBlock) {
        self.cancelEditBlock();
    }
    [self disAppear];
}

- (void)confirmButtonAction {
    if (self.confirmEditBlock) {
        NSString *str = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        self.confirmEditBlock(str,self.selectedColor);
    }
    [self disAppear];
}

- (void)disAppear{
    [self.textView resignFirstResponder];
    self.hidden = YES;
    self.textView.text = @"";
    [self removeFromSuperview];
}

- (void)appear{
    self.hidden = NO;
    [self.textView becomeFirstResponder];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
 
}



@end
